import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Controls from '#cpt/temp/Controls'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== Declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const ProjectModal 		= $Lazy.state(()=>import('./project-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data: [],
		pag: {},
		selectedKeys: [],
		forms : [
			{ label: '项目代码',  	name: 'kind_code'},
			{ label: '项目名称',  	name: 'kind_name'},
			{ label: '项目分类',		name: 'category',	type:'select', loading:true },
		],
	}
	query = {}
	model = {}
	componentDidMount(){
		this.getSelect()
	}
	
	getProject = (uuid) => {
		this.query = {act_id:uuid}
		this.fetch()
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 项目分类
			$fn.setRow(forms, 'category',  await f.project_classify)
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this, 'act-conf-marketing/kindlists',)
	
	// 表单
	cols = [
		{ title: '项目分类', 		dataIndex: 'category_name', 	width:100 },
		{ title: '项目代码', 		dataIndex: 'kind_code', 		width:100 },
		{ title: '项目名称', 		dataIndex: 'kind_name', 		width:250 },
		{ title: '项目属性', 		dataIndex: 'kind_type_name', 	width:180 },
		{ title: '检测方法', 		dataIndex: 'check_method_name', width:180 },
		TableControls.controls.call(this, { width: 60 })
	]
	
	table_controls = (text, { uuid }) => [
		Controls.submit.call(this, '删除', 'act-conf-marketing/unbindkind', { param: {act_id: this.query.act_id, kind_ids:[uuid]},onRefresh:this.fetch} ),
	]
	
	controls = () =>  [
		Controls.click.call(this,'添加',()=> this.projectModal.open(this.query.act_id),{disabled:$fn.isEmpty(this.query.act_id)}),
		Controls.submit.call(this,'删除','act-conf-marketing/unbindkind',{param:{act_id:this.query.act_id, kind_ids:this.state.selectedKeys},isKeys:true,onRefresh:this.fetch}),
		Controls.click.call(this,'搜索',()=> this.fetch(),{disabled:$fn.isEmpty(this.query.act_id)})
	]
	
	render(){
		return (
		<>
			{/* 搜索 */}
			{ SearchForm.call(this, {noRefresh:true, noReset:true, controls: this.controls()})}
			{/* 表格 */}
			{ Table.call(this)}
			<ProjectModal onRef={ref=>this.projectModal=ref} onRefresh={this.fetch} />
		</>
		)

	}
}

export default Index